package org.longteng.core.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 封闭springboot自带jackson的json解析
 * <br>对象转json字符串，json转对象/map
 * @author hymn
 */
@Slf4j
public class JSONObjectUtil {

    // 默认jackson对象
    private static ObjectMapper defaultObjectMapper = new ObjectMapper();
    //根据项目需要自定义配置
    static {
        defaultObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) //解决SerializationFeature.FAIL_ON_EMPTY_BEANS异常
                // .setSerializationInclusion(JsonInclude.Include.NON_NULL) //属性值为null的不参与序列化
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //反序列化时忽略对象中不存在的json字段
        defaultObjectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)   // 设置可用单引号
                .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);      // 设置字段可以不用双引号包括
        defaultObjectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static ObjectMapper getObjectMapper() {
        return defaultObjectMapper;
    }

    //将字符串转成对象
    public static <T> T parseObject(String text, Class<T> clazz) {
        T obj = null;
        if (!StringUtils.isEmpty(text)) {
            try {
                obj = defaultObjectMapper.readValue(text, clazz);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return obj;
    }

    /**
     * @Description  字符串转ObjectNode
     **/
    public static ObjectNode toObjectNode(String text) {
        ObjectNode objectNode = null;
        if (!StringUtils.isEmpty(text)) {
            try {
                objectNode = (ObjectNode) defaultObjectMapper.readTree(text);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return objectNode;
    }

    /**
     * 对象转json, 参照fastjson方法
     * @param object
     * @return
     */
    public static String toJSONString(Object object) {
        return toJSONString(object,false);
    }

    /**
     * 对象转json串
     * @param object
     * @param prettyFormat  是否格式化
     * @return
     */
    public static String toJSONString(Object object, boolean prettyFormat) {
        String res = null;
        try {
            if( prettyFormat) {
                ObjectWriter objectWriter = defaultObjectMapper.writerWithDefaultPrettyPrinter();
                res = objectWriter.writeValueAsString(object);
            } else {
                res = defaultObjectMapper.writeValueAsString(object);
            }
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
        }
        return res;
    }

}
